package com.gong.servlet;

import com.gong.constants.UserConstants;
import com.gong.pojo.User;
import com.gong.service.user.UserService;
import com.gong.service.user.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName: LoginServlet
 * @Description: TODO
 * @Author: 。。。
 * @Data: 2021/10/209:03
 * @Version: 1.0
 **/
public class LoginServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("=============login=============");

		String userCode = req.getParameter("userCode");
		String userPassword = req.getParameter("userPassword");

		UserService userService = new UserServiceImpl();
		User user = userService.getLoginUser(userCode);

		if (user != null && user.getUserpassword().equals(userPassword)) {
			req.getSession().setAttribute(UserConstants.USER_SESSION, user);
			resp.sendRedirect("jsp/frame.jsp");
		} else {
			req.setAttribute("error", "用户名或者密码错误");
			req.getRequestDispatcher("login.jsp").forward(req, resp);
		}


	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}
